<template>
	<view>
		<view class="bgcolor border-radius-bottom"></view>
		<view class="con" style="margin-top:-30px;padding-bottom:120rpx;">
			<view class="titleview ">
				<view>上传照片</view>
				<view class="dflex-a margin-top-sm">
					<view @click="onFront" class="dflex-flow-c dflex">
						<view class="borderimg border dflex-c margin-bottom-xs">
							<image v-if="front_img" class="borderimg" :src="front_img.url"></image>
							<image v-else mode="widthFix" class="imglh" src="/static/upimg.png"></image>
						</view>
						正面
					</view>
					<view @click="onSide" class="dflex-flow-c dflex">
						<view class="borderimg border dflex-c margin-bottom-xs">
							<image v-if="side_img" class="borderimg" :src="side_img.url"></image>
							<image v-else mode="widthFix" class="imglh" src="/static/upimg.png"></image>
						</view>
						侧面
					</view>
					<view @click="onBack" class="dflex-flow-c dflex">
						<view class="borderimg border dflex-c margin-bottom-xs">
							<image v-if="back_img" class="borderimg" :src="back_img.url"></image>
							<image v-else mode="widthFix" class="imglh" src="/static/upimg.png"></image>
						</view>
						后面
					</view>

				</view>
				<view class="topred margin-top-lg">*注意事项</view>
				<image @click="onSide" mode="widthFix" class="w100 margin-top-xs"
					src="https://mp-11e7fbe6-59e1-4cd3-894a-1405ba0c7314.cdn.bspapp.com/ai/info.png"></image>
			</view>
		</view>
		<view @click="tapstart" class="start dflex-c">
			开始量体
		</view>
		<!-- <use-popup :maskCloseAble="false" v-model="show" bgclass=" " centerStyle="" borderRadius="20" mode="center">
			<image mode="widthFix" class="dflex" src="https://mp-11e7fbe6-59e1-4cd3-894a-1405ba0c7314.cdn.bspapp.com/ai/donghua.gif"></image>
		</use-popup> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				front_img: '',
				side_img: '',
				back_img: '',
			}
		},
		onLoad() {
			let info = uni.getStorageSync('aiInfo')
			this.front_img = info.front_imgobj
			this.side_img = info.side_imgobj
			this.back_img = info.back_imgobj
		},
		methods: {
			onFront() {
				this.chooseImages('front')
			},
			onSide() {
				this.chooseImages('side')
			},
			onBack() {
				this.chooseImages('back')
			},
			tapstart() {
				if (!this.front_img) {
					uni.showToast({
						title: "上传正面图",
						icon: "none"
					})
					return
				}
				if (!this.side_img) {
					uni.showToast({
						title: "上传侧面图",
						icon: "none"
					})
					return
				}
				if (!this.back_img) {
					uni.showToast({
						title: "上传背面图",
						icon: "none"
					})
					return
				}

				let info = uni.getStorageSync('aiInfo')
				info.front_img = this.front_img
				info.side_img = this.side_img
				info.back_img = this.back_img
				info.user_id = uni.getStorageSync('usemall_member')._id
				uni.showLoading()
				this.$func.usemall.call('aibody/index/setinfo', {
					all: info
				}).then(res => {
					uni.hideLoading()
					this.show = true
					setTimeout(() => {
						uni.reLaunch({
							url: "./body"
						})
					}, 5000)
				})

			},
			chooseImages(type) {
				console.log(111)
				let _this = this;
				uni.chooseImage({
					count: 1,
					success: async (res) => {
						uni.showLoading({
							title: '上传中',
							mask: true
						});

						for (var i = 0; i < res.tempFilePaths.length; i++) {
							let filename = res.tempFilePaths[i];
							if (res.tempFiles[i] && res.tempFiles[i].name) {
								filename = res.tempFiles[i].name;
							}
							const result = await uniCloud.uploadFile({
								filePath: res.tempFilePaths[i],
								cloudPath: this.$api.getFileName(filename),
								onUploadProgress: function(progressEvent) {
									var percentCompleted = Math.round((progressEvent.loaded *
										100) / progressEvent.total);
								}
							}).then(async uRes => {
								uRes.url = uRes.fileID;
								// 兼容腾讯云上传文件
								if (uRes.url.indexOf('cloud://') != -1) {
									const tempRes = await uniCloud.getTempFileURL({
										fileList: [uRes.url]
									});

									if (tempRes.fileList && tempRes.fileList.length > 0) {
										uRes.url = tempRes.fileList[0].tempFileURL;
									}
								}

								uRes.type = res.tempFiles[i].type || 'image/';
								uRes.size = res.tempFiles[i].size;
								if (type == 'front') {
									_this.front_img = uRes
								} else if (type == 'side') {
									_this.side_img = uRes
								} else {
									_this.back_img = uRes
								}

							}).catch(err => {
								uni.showToast({
									title: "上传失败，重新上传",
									icon: "none"
								})
								console.log('use-upload', err);
							});
						}

						uni.hideLoading();
					}
				});
			}
		}
	}
</script>

<style>
	.start {
		position: fixed;
		width: 100%;
		bottom: 0;
		color: #fff;
		font-weight: bold;
		height: 90rpx;
		background: #00724C;
	}

	.w100 {
		width: 100%;
	}

	.topred {
		font-size: 24rpx;
		color: red;
	}

	.imglh {
		width: 140rpx;
	}

	.borderimg {
		width: 168rpx;
		height: 300rpx;
		border-radius: 16px;
	}

	.border {
		border: 2px solid #8E8E8E;
	}

	.con {
		width: 92%;
		margin: auto;
	}

	.titleview {
		border-radius: 10px;
		padding: 10px;
		background: #fff;
	}

	.bgcolor {
		background: #00724C;
		height: 44px;
	}
</style>